import ModelAbstract from './modelAbstract'
import { image } from '../service/image'
import { directionEnum } from '../enum/directionEnum'
import _ from 'lodash'
import type { MapKey } from '../service/image'
import tank from '../canvas/tank'
import util from '../util'
// import config from '../config'

export default class Tank extends ModelAbstract implements IModel {
  public canvas: ICanvas = tank
  name: string = 'tank'
  render() {
    this.move()
    // 增加坦克下移的概率
    if (_.random(20) === 1) {
      this.direction = directionEnum.bottom
    }
  }
  move(): void {
    const flag = true
    while (flag) {
      let x = this.x
      let y = this.y
      switch (this.direction) {
        case directionEnum.top:
          y--
          break
        case directionEnum.right:
          x++
          break
        case directionEnum.bottom:
          y++
          break
        case directionEnum.left:
          x--
          break
      }
      if (util.isModelTouch(x, y) || util.isCanvasTouch(x, y)) {
        this.randomDirection()
      } else {
        this.x = x
        this.y = y
        break
      }
    }
    super.draw()
  }

  image() {
    const direction = (this.name + _.upperFirst(this.direction)) as MapKey
    return image.get(direction)!
  }
}
